import * as cc from 'cc';
import { ClipGroup } from './ClipGroup';
import { AudioManager } from 'db://assets/component/audio/AudioManager';
const { ccclass, property } = cc._decorator;

enum AudioType {
    music,
    source,
}

function getType(type: AudioType) {
    return AudioType[type];
}

@ccclass('AudioTest')
export class AudioTest extends cc.Component {
    @property(ClipGroup)
    group: ClipGroup = null!;

    @property(cc.ProgressBar)
    progress: cc.ProgressBar = null!;

    onLoad() {}

    start() {}

    update(deltaTime: number) {
        const progress = AudioManager.Instance.getProgress(
            this.group.clip[1],
        );
        const duration = this.group.clip[1].getDuration();
        if (!progress || duration === 0) {
            return;
        }
        if (this.progress) {
            this.progress.progress = cc.math.clamp01(progress / duration);
        }
    }

    onButtonPlay() {
        AudioManager.Instance.play(this.group.clip[0]);
        AudioManager.Instance.play(this.group.clip[1]);
    }

    onButtonStop() {
        AudioManager.Instance.stop(this.group.clip[0]);
        AudioManager.Instance.stop(this.group.clip[1]);
    }

    onButtonPause() {
        AudioManager.Instance.pause(this.group.clip[0]);
        AudioManager.Instance.pause(this.group.clip[1]);
    }

    onButtonResume() {
        AudioManager.Instance.resume(this.group.clip[0]);
        AudioManager.Instance.resume(this.group.clip[1]);
    }

    onSliderSet(slider: cc.Slider) {
        const current = slider.progress * this.group.clip[1].getDuration();
        console.log(current, this.group.clip[1].getDuration());
        AudioManager.Instance.setProgress(this.group.clip[1], current);
    }

    onSliderVolumeSet(slider: cc.Slider) {
        AudioManager.Instance.setVolume(
            this.group.clip[1],
            slider.progress,
        );
    }

    onSliderMainVolume(slider: cc.Slider) {
        AudioManager.Instance.setMainVolume(slider.progress);
    }
}
